//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.utils.basehelper.encrypt;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Img2Base64Util {
    private static final Logger log = LoggerFactory.getLogger(Img2Base64Util.class);

    public Img2Base64Util() {
    }

    public static String getImgByUrl(String urlStr) {
        byte[] data = null;

        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            InputStream inputStream = conn.getInputStream();
            data = readInputStream(inputStream);
            inputStream.close();
        } catch (Exception var5) {
            log.error("文件下载失败" + var5.getMessage());
            var5.printStackTrace();
        }

        return new String(Base64.encodeBase64(data));
    }

    public static String getImgStr(String filePath) {
        InputStream in = null;
        byte[] data = null;

        try {
            in = new FileInputStream(filePath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return new String(Base64.encodeBase64(data));
    }

    public static boolean generateImage(String imgStr, String imgFilePath) {
        if (imgStr == null) {
            return false;
        } else {
            try {
                byte[] b = Base64.decodeBase64(imgStr);

                for(int i = 0; i < b.length; ++i) {
                    if (b[i] < 0) {
                        b[i] = (byte)(b[i] + 256);
                    }
                }

                OutputStream out = new FileOutputStream(imgFilePath);
                out.write(b);
                out.flush();
                out.close();
                return true;
            } catch (Exception var4) {
                return false;
            }
        }
    }

    public static InputStream base64ToInputStream(String base64) {
        ByteArrayInputStream stream = null;

        try {
            byte[] bytes = Base64.decodeBase64(base64);
            stream = new ByteArrayInputStream(bytes);
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return stream;
    }

    public static byte[] readInputStream(InputStream is) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        boolean var3 = true;

        try {
            int length;
            while((length = is.read(buffer)) != -1) {
                baos.write(buffer, 0, length);
            }

            baos.flush();
        } catch (IOException var7) {
        }

        byte[] data = baos.toByteArray();

        try {
            is.close();
            baos.close();
        } catch (IOException var6) {
        }

        return data;
    }
}
